<?php
// +----------------------------------------------------------------------
// | 杭州十禾信息科技有限公司
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2015 http://www.shelpe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: LeePuman <ysyzlsm@163.com>
// +----------------------------------------------------------------------

/**
 * Newzan Action控制器基类
 */
class NewzanAction extends Action
{
	//登陆的情况自动初始化用户信息
	public function _initialize()
	{		
		$this->me = session('?me') ? session('me') : null;		
		$this->manager = session('?manager') ? session('manager') : null;
		$this->customers = session('?customers') ? session('customers') : null;
        // init the controller and action
        $this->controller = strtolower(MODULE_NAME);
        $this->act = strtolower(ACTION_NAME);
        $this->javascript = array();
        import("@.Library.ParamsCheckUtils");
        $this->current_user_id = $this->me['user_id'];
		
	}
    
    public function loadJS()
    {
        $js = array();
        $files = func_get_args();
        foreach($files as $f){
        	$js[] =  $f . ".js";
        }
        $this->javascript = array_merge($this->javascript , $js);
    }

	public function _clearError()
	{
	}

	public function updateUser()
	{
		if($this->me){
			$user = D('User')->getUserInfoById($this->me['uid']);
			session('me' , $user); 
			$this->me = $user;
		}
	}

	public function trail()
	{
	}
	
	public function display_json($r , $code = RetStatus::OK)
	{
		$dom = $this->_get('dom');
		echo json_encode(array(
			'status' => $code,
			'data' => $r,
			'dom' => $dom
		));
		exit;
	}
	

	public function display_json_fastcgi($r , $code = RetStatus::OK)
	{
		$dom = $this->_get('dom');
		echo json_encode(array(
				'status' => $code,
				'data' => $r	
		));
		
	}
	
	public function error( $msg , $code = 500 , $ajax=false)
	{
	    if( $this->isAjax() && $ajax === false){
	        $this->display_json( array('msg' => $msg) , $code );
	    }else{
	        parent::error($msg);
	    }
	}
	
	
	public function display_jsonp($r , $code = RetStatus::OK)
	{
		$dom = $this->_get('dom');
		// $response = $this->getResponse();
		// $response->setHeader('Content-type','application/x-javascript; charset=utf-8', true);
		// $response->sendResponse();
		header("Content-Type: application/x-javascript; charset=UTF-8");
		//header('Access-Control-Allow-Origin: *');
		echo 'jsonpCallback(' 
			. json_encode(array(
				'status' => $code,
				'data' => $r,
				'dom' => $dom
			))
			. ')';
	}
}
